/*
 * i2c_unit.c
 *
 *  Created on: 2021年10月25日
 *      Author: zwr
 */
/*
#include <stdio.h>
#include "LPC8xx.h"
#include "utilities.h"
#include "i2c.h"
#include "stdint.h"
#include "syscon.h"
#include "swm.h"
#include "utilities.h"
#include "iocon.h"
#include "stdlib.h"
#include "time.h"


#define I2CSCL_H        LPC_GPIO_PORT->B0[10] = 1
#define I2CSCL_L        LPC_GPIO_PORT->B0[10] = 0
#define I2CSDA_H        LPC_GPIO_PORT->B0[11] = 1
#define I2CSDA_L        LPC_GPIO_PORT->B0[11] = 0

#define I2CSCL_PIN          P0_10         // P0_10 for I2C0
#define I2CSCL_IOCON_REG    PIO0_10
#define I2CSDA_PIN          P0_11         // P0_11 for I2C0
#define I2CSDA_IOCON_REG    PIO0_11
typedef enum {FALSE = 0, TRUE = !FALSE} Bool;

unsigned int Temperature; //温度
unsigned char ZorF; //正还是负

static __inline _Bool Chip(uint32_t pin) // check whether sensor send an ack to mcu.
{
  // LPC_GPIO_PORT->DIR0 &= ~((1<<SDA_PIN)|(1<<SCL_PIN)); // Input GPIOs to input mode 输入模式
	return (_Bool) LPC_GPIO_PORT->B0[pin];
}


void I2C_delay(void)
{
   uint8_t i=100; //50
   while(i)
   {
     i--;
   }
}


uint8_t I2C_Start(void)
{
	I2CSDA_H;
	I2CSCL_H;
	I2C_delay();
	if(!Chip(11))
		return FALSE;	//SDAÏßÎªµÍµçÆ½Ôò×ÜÏßÃ¦,ÍË³ö

	I2CSDA_L;
	I2C_delay();
	if(Chip(11))
		return FALSE;	//SDAÏßÎª¸ßµçÆ½Ôò×ÜÏß³ö´í,ÍË³ö
	I2CSDA_L;
	I2C_delay();
	return TRUE;
}

void I2C_Stop(void)// iic end signal
{
	I2CSCL_L;
	I2C_delay();
	I2CSDA_L;
	I2C_delay();

	I2CSCL_H;
	I2C_delay();
	I2CSDA_H;
	I2C_delay();
}

void I2C_Ack(void)
{
	I2CSCL_L;
	I2C_delay();
	I2CSDA_L;
	I2C_delay();
	I2CSCL_H;
	I2C_delay();
	I2CSCL_L;
	I2C_delay();
}

void I2C_NoAck(void)
{
	I2CSCL_L;
	I2C_delay();
	I2CSDA_H;
	I2C_delay();
	I2CSCL_H;
	I2C_delay();
	I2CSCL_L;
	I2C_delay();
}

uint8_t I2C_WaitAck(void)
{
	I2CSCL_L;
	I2C_delay();
	I2CSDA_H;//先拉高，等待从机拉低

	I2C_delay();
	I2CSCL_H;  //拉高时钟，等待从机拉低数据线
	I2C_delay();
	if(Chip(11))//若仍为高则无ack
	{
		I2CSCL_L;
      return FALSE;
	}
	I2CSCL_L;
	return TRUE;//收到从机应答ack
}
*/
/*
void I2C_GPIO_Config(void){

	   LPC_SYSCON->SYSAHBCLKCTRL[0] |= (I2CINST|SWM|IOCON); //(IOCON|GPIO|I2C0);
	    // Configure the SWM
	   LPC_SWM->PINENABLE0 &= ~(I2C0_SCL|I2C0_SDA);
	   LPC_SYSCON->PRESETCTRL[0] &= (~(1<<6) & ~(1<<20));  //GPIO初始化  GPIO0_RST_N   GPIO1_RST_N
	   LPC_SYSCON->PRESETCTRL[0] |= ~(~(1<<6) & ~(1<<20));
	    // Give I2C a reset
	   LPC_SYSCON->PRESETCTRL0 &=  (I2CINSTRSTN);
	   LPC_SYSCON->PRESETCTRL0 |= ~(I2CINSTRSTN);

	   SCL_H;
	   SDA_H;
	   LPC_GPIO_PORT->SET0  = (1<<SCL_PIN)|(1<<SDA_PIN);
	   LPC_GPIO_PORT->DIR0 |= (1<<SCL_PIN)|(1<<SDA_PIN);  // 输出模式

	   // Enable main_clk as function clock to I2C
	   LPC_SYSCON->I2CINSTCLKSEL = FCLKSEL_MAIN_CLK;  //LPC_SYSCON->I2C0 = 1;
	     // Get main_clk frequency
	   SystemCoreClockUpdate();
		  // Configure the I2C clock divider 配置I2C时钟分频器
	     // Use default clock high and clock low times (= 2 clocks each)
	  // Thus, 4 main_clk periods for each I2C bit time.
	  // So the I2C clock divider must produce main_clk/4*I2CBAUD clocks per second.
	  // Remember, value written to DIV divides by value+1
	   pI2CINST->DIV = (main_clk/(4*I2CBAUD)) - 1;
	    // Configure the I2C CFG register:
	  // Master enable = true
	  // Slave enable = false
	  // Monitor enable = false
	  // Time-out enable = false
	  // Monitor function clock stretching = false
	  //
	  pI2CINST->CFG = CFG_MSTENA;

}
*/
